--- permalink: /python/offline description: How to use Plotly offline inside IPython notebooks with Plotly Offline title: Plotly Offline for IPython Notebooks has_thumbnail: false thumbnail: /images/static-image has_thumbnail: false layout: user-guide page_type: example_index language: python --- {% raw %}
Plotly Offline brings interactive Plotly graphs to the offline (local) environment.
Instead of saving the graphs to a server, your data and graphs will remain in your local system. When you're ready to share, you can just publish them to the web with an online Plotly account or to your company's internal Plotly Enterprise.
To get started with Plotly Offline, upgrade to the 1.9.x series:
! pip install plotly --upgrade
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
print __version__ # requires version >= 1.9.0
You can plot your graphs from a python script from command line. On executing the script, it will open a web browser with your Plotly Graph drawn.
from plotly.offline import plot
from plotly.graph_objs import Scatter
plot([Scatter(x=[1, 2, 3], y=[3, 1, 6])])
You can also plot your graphs offline inside a Jupyter(IPython) Notebook Environment. First you need to initiate the Plotly Notebook mode as below:
init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
# this injects the plotly.js source files into the notebook
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
from plotly.graph_objs import *
import numpy as np
iplot([Box(y = np.random.randn(50), showlegend=False) for i in range(45)], show_link=False)
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
# Plotting with Pandas
import pandas as pd
df = pd.read_csv('https://plot.ly/~etpinard/191.csv')
df.head(1)
iplot({
'data': [
Scatter(x=df[continent+'_Life Expentancy [in years]'],
y=df[continent+'_Gross Domestic Product per Capita [in USD of the year 2000]'],
text=df[continent+'_text'],
marker=Marker(size=df[continent+'_marker.size'], sizemode='area', sizeref=131868,),
mode='markers',
name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
],
'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
}, show_link=False)
# An alternative method for Pandas plotting is with cufflinks: https://github.com/santosjorge/cufflinks
! pip install cufflinks --upgrade
import cufflinks as cf
iplot(cf.datagen.lines().iplot(asFigure=True,
kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))
iplot(cf.datagen.heatmap(20,20).iplot(asFigure=True,
kind='heatmap',colorscale='spectral',title='Cufflinks - Heatmap'))
import pandas as pd
df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()
airports = [ dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = df_airports['long'],
lat = df_airports['lat'],
hoverinfo = 'text',
text = df_airports['airport'],
mode = 'markers',
marker = dict(
size=2,
color='rgb(255, 0, 0)',
line = dict(
width=3,
color='rgba(68, 68, 68, 0)'
)
))]
flight_paths = []
for i in range( len( df_flight_paths ) ):
flight_paths.append(
dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = [ df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i] ],
lat = [ df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i] ],
mode = 'lines',
line = dict(
width = 1,
color = 'red',
),
opacity = float(df_flight_paths['cnt'][i])/float(df_flight_paths['cnt'].max()),
)
)
layout = dict(
title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
showlegend = False,
height = 800,
geo = dict(
scope='north america',
projection=dict( type='azimuthal equal area' ),
showland = True,
landcolor = 'rgb(243, 243, 243)',
countrycolor = 'rgb(204, 204, 204)',
),
)
fig = dict( data=flight_paths + airports, layout=layout )
iplot(fig)
import plotly.plotly as py # all methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise
# get_figure downloads a figure from plot.ly or Plotly Enterprise.
# You need to provide credentials to download figures: https://plot.ly/python/getting-started/
fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True)
iplot(fig)